<?php
/**
 * Created by PhpStorm.
 * User: yangxu
 * Date: 2016/5/13
 * Time: 16:57
 */

namespace Admin\Controller;


use Think\Controller;

class TestKeyController extends Controller{
    private $testkey;
    public function __construct(){
        parent::__construct();
        if(!logAdmin(session('adminId'))) header('location:'.U('Admin/Index/login'));
        $this->testkey=M('Testkey');
    }
   
    
    public function testkey_cata_list(){
        if(IS_GET){
            $type=I('get.level_one',1);
            $result=D('Testkey')->getCata($type);
            $CataOne=$this->getCataOne();
            $this->assign('CataOne',$CataOne);
            $this->assign('level_one_hid',$type);
            $this->assign('cata_list',$result['list']);
            $this->assign('page',$result['show']);
            $this->display('TestKey_Cata_list');
        }
    
    }
    
    public function getCataOne(){
        $result=M('category')->where('level=1')->select();
        if($result){
            return $result;
        }
    }
    
    public function Testkey_list(){
        $re=I('get.');
        $map['cata_id']=$re['cat_two'];
        $TestKeyList=$this->testkey->where($map)->select();
        $this->assign('cata_id',$re['cat_two']);
        $this->assign('TestKey_List',$TestKeyList);
        $this->display();
    }
    
     public function index(){
        $page=isset($_GET['p'])?$_GET['p']:1;
        $count=$this->testkey->count();
        vendor('Page');
        $pageClass=new \Page();
        $totalPage = ceil($count/10);
        $show=$pageClass->PageCut($page,$totalPage,$count,10);
        $this->assign('page',$show);
        $keylist=$this->testkey->page($page,10)->select();
        $this->assign('keylist',$keylist);


        $this->display();
    }
       
    //设置全套购买优惠价格
    public function setPrice(){
        if(IS_GET){
            $cat_id=I('get.cata_id');
            $price=M('price')->where(['cate_id'=>$cat_id])->find();
            if(empty($price)){
                $data['cate_id']=$cat_id;
                $re=M('price')->add($data);
            }
            $this->assign('cat_id',$cat_id);
            $this->assign('price',$price['testkey_price']);
            $this->display('TestKey/setPrice');
        }else{
            $cat_id=I('post.cat_id');
            $price=I('post.price');
            $data['testkey_price']=$price;
            $re=M('price')->where(['cate_id'=>$cat_id])->save($data);
            $re?$this->success('设置价格成功',U('Testkey_list',['cat_two'=>$cat_id])):$this->error('未做任何修改');
        }
    }
    
    public function addProtocol(){
        if(IS_GET){
        $cate_id=I('get.cata_id');
        $re=M('adsmanage')->where(['cat_id'=>$cate_id])->field('cat_id')->find();
        if(empty($re)){
            $data['cat_id']=$cate_id;
            M('adsmanage')->add($data);
            $this->assign('cata_id',$cate_id);
            $this->display('TestKey/addProtocol');
        }else{
            $re= M('adsmanage')->where(['cat_id'=>$cate_id])->field('protocol')->find();
            $this->assign('protocol',$re['protocol']);
            $this->assign('cata_id',$cate_id);
            $this->display('TestKey/addProtocol');
        }
        
        }else{
            $protocol=I('post.protocol');
            $cate_id=I('post.cata_id');
            $data['protocol']=$protocol;
            $re=M('adsmanage')->where(['cat_id'=>$cate_id])->save($data);
            $re>=0?$this->success('编辑协议成功',U('Testkey_list',['cat_two'=>$cate_id])):$this->error('编辑押密协议失败');
        }
    }
    
    public function addTestKey(){
        if($_POST){
            $data=array(
                'title'=>$_POST['name'],
                'content'=>$_POST['content'],
                'price'=>$_POST['price'],
                'cata_id'=>$_POST['cata_id'],
                'addtime'=>time()
            );
            $cata_id=$_POST['cata_id'];
            if($this->testkey->add($data)){
                $this->success('添加成功',U('Testkey_list',['cat_two'=>$cata_id]));
            }else{
                $this->success('添加失败');
            }
        }else{
            $cata_id=$_GET['cata_id'];
            $this->assign('cata_id',$cata_id);
            $this->display('add');
        }
    }
    public function editTestKey(){
        if($_POST){
            $data=array(
                'title'=>$_POST['name'],
                'content'=>$_POST['content'],
                'price'=>$_POST['price'],
            );
            $cata_id=$_POST['cata_id'];
            $id=$_POST['id'];
          if($this->testkey->where(['id'=>$id])->save($data)>=0){
                $this->success('编辑成功',U('Testkey_list',['cat_two'=>$cata_id]));
            }else{
                $this->success('编辑失败');
            }
        }else{
            $keyli=$this->testkey->where('id='.$_GET['id'])->find();
            $this->assign('keyli',$keyli);
            $this->display('edit');
        }
    }
    public function delTestKey(){
        if($this->testkey->where('id='.$_GET['id'])->delete()){
            $this->success('删除成功');
        }else{
            $this->success('删除失败');
        }
    }
    /**
     * 批量删除押密
     */
    public function delete_all(){
        if(IS_GET){
            $info=I('get.');
            if(!empty($info)){
                $re=explode('@',$info['ids']);
                if(!empty($re)){
                    $map['id']=array('in',$re);
                    $result=$this->testkey->where($map)->delete();
                    $result=$result?true:false;
                    $this->ajaxReturn($result);
                }
                else{
                    $this->ajaxReturn(false);
                }
            }else{
                $this->ajaxReturn('提交信息出错');
            }
        }
    }
    /**
     * 批量删除押密
     */
    public function allDel(){
        $id=$_GET['actionid'];
        $id=explode('-',$id);
        $where['id']=array('in',$id);
        if($this->testkey->where($where)->delete()){
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }
}